#include <func.h>
#include "token.h"
#include "virtualFileSystem.h"
#include "head.h"
#include "account.h"
int firstConnect(int netFd,clientMsg_t *client,char *tokenSalt){
        client->netFd = netFd;
        //用户登录界面
        //服务端与客户端共存一个用户名
        char *userName = (char *)calloc(50,sizeof(char));
        loadSever(netFd,&userName);
        strcpy(client->userName,userName);
        //客户端与服务端共存一个路径地址
        //获取当前用户的根目录
        client->path[0] = getOriginId(userName);
        client->pathCount = 0;
        //接收客户端登录时间
        train_t t;
        bzero(&t,sizeof(t));
        recv(netFd,&t.dataLength,sizeof(t.dataLength),0);
        recv(netFd,t.buf,t.dataLength,0);
        //组合用户名以及登录时间来生成token
        strcat(t.buf,userName);
        //生成token密文
        char token[200] = {0};
        strcpy(token,crypt(t.buf,tokenSalt));
        //输出token密文用于测试对比
        puts(token);
        //发送token密文发给客户端保存
        bzero(&t,sizeof(t));
        t.dataLength = strlen(token);
        strcpy(t.buf,token);
        printf("send datalength = %d\n",t.dataLength);
        printf("send t.buf = %s\n",t.buf);
        send(netFd,&t,sizeof(t.dataLength) + t.dataLength,0);
        free(userName);
}           


int shortCommond(int netFd,clientMsg_t client,int epfd){

         char command_buf[1024]={0};
         int ret = recv(netFd,command_buf,sizeof(command_buf),MSG_NOSIGNAL);
         if(ret == 0){
            puts("客户端关闭，结束连接");
            epollDel(client.netFd,epfd);
             return 0;
            }
         int command = atoi(command_buf);

         if(command == 0){
             return 0;
         }
         printf("收到指令%d\n",command);

         char pathBuf[1024]={0};
         switch (command)
         {
         case 1:{
             func1_ls(netFd,client.path[client.pathCount]);
             break;
         }

         case 2:
            //待修改
             func2(pathBuf, netFd);
             break;

         case 3:
             func3(netFd);
             break;

         case 4:
             //func4(netFd);
             break;

         case 5:
             //func5(netFd);
             break;

         case 6:
             func6(netFd);
             break;

         case 7:
             func7(netFd);
             break;

         case 8:
             //func8();
             break;

         default:
             printf("error\n");
             break;
         }

}

int func1_ls(int netFd,int pre_id){
    //ls_c(netFd);
    lsSever(netFd,pre_id);
}
//待修改
int func2(char *pathBuf, int netFd){
    recv(netFd,pathBuf,sizeof(pathBuf),0);
    puts(pathBuf);
    cd_c(pathBuf, netFd);
}

int func3(int netFd){
    pwd(netFd);
}

int func4(int netFd){
    uploadSever(netFd,"shuai",3);
    //recvFile(netFd);
}

int func5(int netFd){
    downSever(netFd,3);
    // char buf[1024]={0};
    // bzero(buf,sizeof(buf));
    // recv(netFd,buf,sizeof(buf),0);
    // sendFile(buf,netFd);//传入文件名
}

int func6(int netFd){
    remove_net(netFd);
}

int func7(int netFd){
    viewlog(netFd);
}
